package com.njtoyo.taxi.admin.service.business.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.logic.business.order_refund.RefundService;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverDeviceAutoPartnerInformation;
import com.njtoyo.taxi.admin.mapper.master.entity.Ride;
import com.njtoyo.taxi.admin.mapper.master.entity.User;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverDeviceAutoPartnerInformationMapper;
import com.njtoyo.taxi.admin.rest.pojo.dto.DriverDeviceAddDTO;
import com.njtoyo.taxi.admin.rest.wrapper.business.driver.DriverDeviceAutoPartnerQueryWrapper;
import com.njtoyo.taxi.admin.service.business.DriverDeviceAutoPartnerService;
import com.taxi.entity.common.RestResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class DriverDeviceAutoPartnerServiceImpl implements DriverDeviceAutoPartnerService {
    @Resource
    private DriverDeviceAutoPartnerInformationMapper driverDeviceAutoPartnerMapper;

    @Override
    public RestResult<Page<DriverDeviceAutoPartnerInformation>> getList(DriverDeviceAutoPartnerQueryWrapper wrapper) {
        LambdaQueryChainWrapper<DriverDeviceAutoPartnerInformation> rideLambdaQueryChainWrapper =
                new LambdaQueryChainWrapper<>(driverDeviceAutoPartnerMapper);
        rideLambdaQueryChainWrapper.like(ObjectUtil.isNotEmpty(wrapper.getName()), DriverDeviceAutoPartnerInformation::getName, wrapper.getName());
        rideLambdaQueryChainWrapper.eq(ObjectUtil.isNotEmpty(wrapper.getDeviceModel()), DriverDeviceAutoPartnerInformation::getDeviceModel, wrapper.getDeviceModel());
        Page<DriverDeviceAutoPartnerInformation> page = new Page<>(wrapper.getCurrent(), wrapper.getSize());
        Page<DriverDeviceAutoPartnerInformation> res = rideLambdaQueryChainWrapper.
                orderByDesc(DriverDeviceAutoPartnerInformation::getCreatedAt).page(page);
        return RestResult.success(res);
    }

    @Override
    public RestResult add(DriverDeviceAddDTO dto) {
        String name = dto.getName();
        QueryWrapper<DriverDeviceAutoPartnerInformation> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        DriverDeviceAutoPartnerInformation info = driverDeviceAutoPartnerMapper.selectOne(wrapper);
        if (ObjectUtil.isNotEmpty(info)) {
            return RestResult.failed("已存在同名车机设备厂商,请勿重复添加!");
        }
        DriverDeviceAutoPartnerInformation entity = new DriverDeviceAutoPartnerInformation();
        entity.setCreatedAt(new Date());
        entity.setDeviceModel(dto.getDeviceModel());
        entity.setName(name);
        if(StringUtils.isNotBlank(dto.getRemark())){
            entity.setRemark(dto.getRemark());
        }
        driverDeviceAutoPartnerMapper.insert(entity);
        return RestResult.success();
    }
}
